<?php
    class TourController extends Main_Controller_Action
    {
        private $_arrParam;
        //private _tour;
        private $_tour;
        private $_comment;
        private $_orders;
        private $_order_detail;
        
        public function init() {
            $this->loadTemplates('default');
            $this->_arrParam = $this->_request->getParams();
            $this->_tour= new Default_Model_Tour();
            $this->_comment= new Default_Model_Comment();
            $this->_orders=new Default_Model_Orders();
            //$this->_order_detail= new Default_Model_Order_Detail();
        }
        
        public function indexAction() {
            $this->view->headTitle('Tournow - Tour du lịch');
            if(empty($this->_arrParam['cat_id'])) {
                $cat_id = 0;
            }
            else {
                $cat_id = $this->_arrParam['cat_id'];
            }
            $tour=$this->_tour->listTourIndex($cat_id);
            $paginator=Zend_Paginator::factory($tour);
           // set the number of counts in a page
            $paginator->setItemCountPerPage(20)//so record muon hien thi
         			->setPageRange(5)//so page muon hien thi
         			->setCurrentPageNumber($this->_getParam("page", 1));
            
            $this->view->data=$paginator;
        }
        
        public function detailsAction() {
            $this->view->headTitle('Tournow - Chi tiết tour du lịch');
            $tour_id=$this->_request->getParam('tour_id');
            
            //$data=$this->_tour->getTourById($id);
            $tour= new Default_Model_Tour();
            $data=$tour->getTourById2($tour_id);
            $this->view->tour=$data;
            
            // Lay ra nhung comment cho tour nay
            $comments=$this->_comment->getCommentByTourId($tour_id);
            
            $this->view->data=$comments;
            
            if($this->_request->isPost())
            {
                $auth = Zend_Auth::getInstance();
                $infoUser = $auth->getIdentity();
                $term=$this->_request->getParam('content');
                $filter = new Zend_Filter_StringTrim();
                if($filter->filter($term) == '') {
                    $this->view->errorcontent = 'Bạn chưa nhập nội dung bình luận';
                }
                else {
                    $data=array(
                        'term'=>$term,
                        'tour_id'=>$tour_id,
                        'user_id'=>$infoUser->user_id,
                        'create_date' => date('Y-m-d H:i:s'),
                        'status'=>1
                    );
                    $this->_comment->addComment($data);
                    $this->_redirect($_SERVER['HTTP_REFERER']);
                }
            }
            
            // Recommend
            $this->view->recommend = $this->_tour->recommend($tour_id);
    }
    
    public function bookAction()
    {
        $this->view->headTitle('Tournow - Đặt tour du lịch');
        $tour_id = $this->_request->getParam('tour_id');
        $this->view->data = $this->_tour->getTourById($tour_id);
        
        if($this->_request->isPost())
        {   
            // Lay du lieu tu form
            $adult_qty=$this->_request->getParam('adult_qty');
            $children_qty=$this->_request->getParam('children_qty');
            $flag = true;
            
            $validate = new Default_Form_ValidateTour();
            if(!$validate->validateQuantity($adult_qty)){
                $this->view->error = 'Số lượng phải là số và không được để trống';
                $flag = false;
            }
            if(!$validate->validateQuantity($children_qty)){
                $this->view->error = 'Số lượng phải là số và không được để trống';
                $flag = false;
            }
            if($flag == true) {
                $time=date('Y-m-d H:i:s');
                $auth = Zend_Auth::getInstance();
                $userInfo=$auth->getIdentity();
                $user_id=$userInfo->user_id;
                
                $status=0;
                // Du lieu de them vao bang order
                $data1=array(
                    'tour_id'=> $tour_id,
                    'time'=> $time,
                    'status'=> $status,
                    'user_id'=> $user_id
                );
                
                // Them ban ghi vao bang order
                $orders_id=$this->_orders->addOrders($data1);
                
                // Lay cua tour de tinh ra tong chi phi cho order_detail
                $tour=$this->_tour->getTourById($tour_id);
                $total_cost=$tour['adult_price']*$adult_qty+$tour['children_price']*$children_qty;
                
                // Du lieu de them vao bang order_detail
                $data2=array(
                    'order_id'=>$orders_id,
                    'adult_qty'=>$adult_qty,
                    'children_qty'=>$children_qty,
                    'total_cost'=>$total_cost
                );

                // Them du lieu vao bang order_detail
                $order_detail= new Default_Model_OrderDetail();
                $order_detail->addOrderDetail($data2);
                $this->_redirect('tour/index');
            }
        }
    }
    
    public function searchAction(){
        $this->view->headTitle('Tournow - Tìm kiếm tour du lịch');
        $start_place=$this->_request->getParam('start_place');
        $finish_place=$this->_request->getParam('finish_place');
        $start_date=$this->_request->getParam('start_date');
        $price=$this->_request->getParam('price');
        if($start_date != '') {
            $start_date=$this->_tour->convertToMysql($start_date);
        }
        
        // Truy van de lay ve ket qua
        $this->view->data = $this->_tour->searchTour($start_place,$finish_place,$start_date,$price);
    }
   
}
